package com.ithuameng.admin.app.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ithuameng.admin.app.system.model.DictData;
import com.ithuameng.admin.app.system.model.admin.qo.DictDataQo;
import com.ithuameng.admin.app.system.model.vo.AdminPageResVo;
import com.ithuameng.admin.app.system.model.vo.R;
import com.ithuameng.admin.app.system.service.IDictDataService;
import com.ithuameng.admin.app.system.service.IDictTypeService;
import com.ithuameng.admin.enums.CommonError;
import com.ithuameng.admin.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 数据字典信息
 * 
 * @author ithuameng
 */
@Api(tags = "字典数据控制器")
@RestController
@RequestMapping("/admin/v1/dict/data")
public class DictDataController extends AdminBaseController {

	@Resource
	private IDictDataService dictDataService;

	@Resource
	private IDictTypeService dictTypeService;

	@ApiOperation("获取字典数据列表")
	@PreAuthorize("@ss.hasPermi('system:dict:list')")
	@GetMapping("/list")
	public R<AdminPageResVo<DictData>> list(DictDataQo dictDataQo) {
		Page<DictData> page = new Page<>(dictDataQo.getPageNumber(), dictDataQo.getPageSize());
		IPage<DictData> pageList = dictDataService.listDictData(page, dictDataQo);
		AdminPageResVo<DictData> dictDataPage = new AdminPageResVo<>();
		dictDataPage.setRecords(pageList.getRecords());
		dictDataPage.setPage(pageList.getPages());
		dictDataPage.setTotal(pageList.getTotal());
		return new R<>(dictDataPage);
	}

	@ApiOperation("查询字典数据详细")
	@PreAuthorize("@ss.hasPermi('system:dict:list')")
	@GetMapping(value = "/detail/{id}")
	public R<DictData> getInfo(@PathVariable Long id) {
		DictData dictData = dictDataService.getById(id);
		return new R<>(dictData);
	}

	@ApiOperation("根据字典数据查询字典数据信息")
	@GetMapping(value = "/type/{dictType}")
	public R<List<DictData>> dictType(@PathVariable String dictType) {
		List<DictData> data = dictTypeService.listDictDataByType(dictType);
		if (StringUtils.isNull(data)) {
			data = new ArrayList<>();
		}
		return new R<>(data);
	}

	@ApiOperation("新增字典数据")
	@PreAuthorize("@ss.hasPermi('system:dict:add')")
	@PostMapping(value = "/add")
	public R<Boolean> add(@Validated @RequestBody DictData dict) {
		if (dict.getStatus() == null) {
			return new R<>(CommonError.BIZ_ERR, "请选择状态");
		}
		dictDataService.save(dict);
		return new R<>(CommonError.OK, "添加成功");
	}

	@ApiOperation("修改字典数据")
	@PreAuthorize("@ss.hasPermi('system:dict:edit')")
	@PostMapping(value = "/edit")
	public R<Boolean> edit(@Validated @RequestBody DictData dict) {
		if (dict.getStatus() == null) {
			return new R<>(CommonError.BIZ_ERR, "请选择状态");
		}
		dictDataService.updateById(dict);
		return new R<>(CommonError.OK, "修改成功");
	}

	@ApiOperation("删除字典数据")
	@PreAuthorize("@ss.hasPermi('system:dict:remove')")
	@PostMapping("/delete/{dictCodes}")
	public R<Boolean> remove(@PathVariable Long[] dictCodes) {
		dictDataService.removeByIds(Arrays.asList(dictCodes));
		return new R<>(CommonError.OK, "刪除成功");
	}
}
